/**
 * Project Name:my_project
 * File Name:DayUtils.java
 * Package Name:com.lvman.utils
 * Date:2014年10月24日上午10:53:57
 * Copyright (c) 2014.
 */

package com.jams.goodox.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * ClassName:DayUtils <br/>
 * Function: 日期相关工具类 Reason:  ADD REASON. <br/>
 * Date: 2014年10月24日 上午10:53:57 <br/>
 *
 * @author Administrator
 * @see
 * @since JDK 1.6
 */
public class DayUtils {
    // 添加大小月月份并将其转换为list,方便之后的判断
    // 添加大小月月份并将其转换为list,方便之后的判断
    public static String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
    public static String[] months_little = {"4", "6", "9", "11"};

    public final static List<String> list_big = Arrays.asList(months_big);
    public final static List<String> list_little = Arrays.asList(months_little);

    public static Calendar calendar = Calendar.getInstance();
    public static final int year = calendar.get(Calendar.YEAR);
    public static final int month = calendar.get(Calendar.MONTH) + 1;
    public static final int day = calendar.get(Calendar.DATE);
    public static final int hour = calendar.get(Calendar.HOUR_OF_DAY);
    public static final int minute = calendar.get(Calendar.MINUTE);
    public static int setYear = 2006;
    public static int setMonth = 1;
    public static int setDay = 1;
    public static final int seHhour = calendar.get(Calendar.HOUR_OF_DAY);
    public static final int seMminute = calendar.get(Calendar.MINUTE);

    /**
     * 根据当前的年份和月份得到当前的日期
     *
     * @param month
     * @return
     */
    public static int getDay(int year, String month) {
        // 判断大小月及是否闰年,用来确定"日"的数据
        if (list_big.contains(month)) {
            return 31;
        } else if (list_little.contains(month)) {
            return 30;
        } else {
            if (yearStyle(year))
                return 29;
            else
                return 28;
        }
    }

    public static int getTheMonthDay(int year, String month) {
        // 判断大小月及是否闰年,用来确定"日"的数据
        if (list_big.contains(month)) {
            return 31;
        } else if (list_little.contains(month)) {
            return 30;
        } else {
            if (yearStyle(year))
                return 29;
            else
                return 28;
        }
    }

    /**
     * 得到初始化的时间---邻里发布，推后3天
     *
     * @return
     */
    public static int getNeighbourMonth() {
        if (list_big.contains(String.valueOf(month))) {
            if ((31 - day) < 3) {
                if (month == 12) {
                    return 1;
                } else {
                    return month + 1;
                }
            } else {
                return month;
            }
        } else if (list_little.contains(String.valueOf(month))) {
            if ((30 - day) < 3) {
                return month + 1;
            } else {
                return month;
            }
        } else {
            if (yearStyle() && (29 - day) < 3) {
                return month + 1;
            } else if (!yearStyle() && (28 - day) < 3) {
                return month + 1;
            } else {
                return month;
            }
        }
    }

    /**
     * 得到初始化的天--邻里发布，推后三天
     *
     * @return
     */
    public static int getNeighbourDay() {
        if (list_big.contains(String.valueOf(month))) {
            if ((31 - day) < 3) {
                return 3 - (31 - day);
            } else {
                return day + 3;
            }
        } else if (list_little.contains(String.valueOf(month))) {
            if ((30 - day) < 3) {
                return 3 - (30 - day);
            } else {
                return day + 3;
            }
        } else {
            if (yearStyle() && (29 - day) < 3) {
                return 3 - (29 - day);
            } else if (!yearStyle() && (28 - day) < 3) {
                return 3 - (28 - day);
            } else {
                return day + 3;
            }
        }
    }

    public static String getNeighbourEntime(int month, int day, int hours) {
        String parten = "00";
        DecimalFormat decimal = new DecimalFormat(parten);
        String chooseTime = (year + "-" + decimal.format(month) + "-" + decimal.format(day) + " "
                + decimal.format(hours) + ":00");
        if (!Utils.timeCurrentCompare(chooseTime)) {
            chooseTime = ((year + 1) + "-" + decimal.format(month) + "-" + decimal.format(day) + " "
                    + decimal.format(hours) + ":00");
        }
        return chooseTime;
    }

    public static String getTime(int year, int month, int day, int hours, int minute) {
        String part = "00";
        DecimalFormat decimal = new DecimalFormat(part);
        String chooseTime = (year + "-" + decimal.format(month) + "-" + decimal.format(day) + " "
                + decimal.format(hours) + ":" + decimal.format(minute));
        return chooseTime;
    }

    public static String getBirthdayEntime(int year, int month, int day) {
        String parten = "00";
        DecimalFormat decimal = new DecimalFormat(parten);
        String chooseTime = (year + "-" + decimal.format(month) + "-" + decimal.format(day));
        return chooseTime;
    }

    /**
     * 得到初始化的时间--邻里发布
     *
     * @return
     */
    public static int getNeighbourHour() {
        return hour;

    }

    /**
     * 得到初始化的时间--邻里发布
     *
     * @return
     */
    public static int getNeighbourYear() {
        return year;

    }

    public static boolean yearStyle() {
        if (((year) % 4 == 0 && (year) % 100 != 0) || (year) % 400 == 0) {
            // 29天
            return true;
        } else {
            // 28天
            return false;
        }
    }

    public static boolean yearStyle(int year) {
        if (((year) % 4 == 0 && (year) % 100 != 0) || (year) % 400 == 0) {
            // 29天
            return true;
        } else {
            // 28天
            return false;
        }
    }

    public static Calendar stringToCalendar(String strTime) {
        // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();

        try {
            // 对 calendar 设置时间的方法
            // 设置传入的时间格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = dateFormat.parse(strTime);
            // 对 calendar 设置为 date 所定的日期
            calendar.setTime(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar;
    }

    public static String getcurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String formatDate = sdf.format(date);
        return formatDate;
    }

    public static Calendar stringToCalendar(String strTime, String format) {
        // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();

        try {
            // 对 calendar 设置时间的方法
            // 设置传入的时间格式
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            Date date = dateFormat.parse(strTime);
            // 对 calendar 设置为 date 所定的日期
            calendar.setTime(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar;
    }

    public static String longTimeToString(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(time);
    }

    public static String[] getEmptyHours(){
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        String[] hours=new String[24-hour];
        for(int i=0;i<24-hour;i++){
            hours[i]=String.valueOf(hour+i+"点");
        }
        return hours;
    }

    public static String getNowDate(){
        SimpleDateFormat df = new SimpleDateFormat("MM-dd");
        Calendar calendar=Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    /**
     * 判断选中时间是否早于当前时间
     * @param chooseDateTime 选中的时间字符串
     * @return true:早 false 晚
     */
    public static boolean isBeforeNowTime(String chooseDateTime){
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Calendar calendar=Calendar.getInstance();
            calendar.set(Calendar.MILLISECOND,0);

            Date date =df.parse(String.format("%s-%s",calendar.get(Calendar.YEAR),chooseDateTime));
            Calendar calendars=Calendar.getInstance();
            calendars.setTime(date);
            return calendars.compareTo(calendar)==-1;
        }catch (Exception e){
            return true;
        }
    }

    public static String[] getNextMonthArray(long curDateLong){
        try{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String[] nextMonthArray=new String[30];
            Calendar calendar=Calendar.getInstance();
            calendar.setTimeInMillis(curDateLong);
            nextMonthArray[0]=sdf.format(calendar.getTime());
            for (int i=1;i<30;i++){
                calendar.add(Calendar.DATE,1);
                nextMonthArray[i]=sdf.format(calendar.getTime());
            }
            return nextMonthArray;
        }catch (Exception e){
            return new String[]{""};
        }
    }
}
